<?php

class MenusModule extends fvModule {

    private $appName = 'frontend';
    private $appConfig = null;
    
    function __construct () {
        $this->appConfig = new fvConfig(fvSite::$fvConfig->get("path.application.{$this->appName}.config"));
        $this->appConfig->Load("modules.yml");
        $this->appConfig->Load("template.yml");
        
        $moduleName = strtolower(substr(__CLASS__, 0, -6));

        parent::__construct(fvSite::$fvConfig->get("modules.{$moduleName}.smarty.template"), 
                            fvSite::$fvConfig->get("modules.{$moduleName}.smarty.compile"), 
                            fvSite::$Layoult);
    }

    function showIndex() {
        if (!fvRequest::getInstance()->hasRequestParameter("page")) {
            if (fvSite::$fvSession->is_set(fvRequest::getInstance()->getRequestParameter("requestURL")."/page")) {
                fvRequest::getInstance()->putRequestParameter("page",
                    (int)fvSite::$fvSession->get(fvRequest::getInstance()->getRequestParameter("requestURL")."/page"));
            } 
        } else {
            fvSite::$fvSession->set(fvRequest::getInstance()->getRequestParameter("requestURL")."/page", 
                fvRequest::getInstance()->getRequestParameter("page"));
        }

        if (substr($requestURL = fvRequest::getInstance()->getRequestParameter("requestURL"), 0, 1) == "/") {
            $requestURL = substr($requestURL, 1);
        }
        
        $MenuManager = MenuManager::getInstance();
        //fvSite::$fvConfig->get("dir_web_root") . $requestURL . 
        //$ManagerParams = ManagerParamManager::getInstance()->getAll();
        
        $this->__assign('Menus', $MenuManager->getMenuTree());
        
        $request = fvRequest::getInstance();
        if (!$Menu = MenuManager::getInstance()->getByPk($request->getRequestParameter('id'))) {
            $Menu = new Menu();
        }
        
        $this->__assign(array(
            'Menu' => $Menu,
            'MenuManager' => MenuManager::getInstance(), 
        ));
        
        return $this->__display('menu_list.tpl');
    }

    function showUploadimage() {
        if (fvParams::getInstance()->getParameter(fvRoute::getInstance()->getModuleName() . "/filename")) {
            $info = fvMediaLib::calcDementions(fvSite::$fvConfig->get('path.upload.tmp_dir') . fvParams::getInstance()->getParameter(fvRoute::getInstance()->getModuleName() . "/filename"),
                                                array('type' => Menu::IMAGE_TYPE_SMALL));
            
            return "<script>
                var fileName = '" . fvParams::getInstance()->getParameter(fvRoute::getInstance()->getModuleName() . "/filename")."';
                var width = ". $info[0] .";
                var height = ". $info[1] .";
                
                if (top.updateImage) {
                    top.updateImage(fileName, width, height);
                }
            </script>";
        } else {
            return "<script>
                alert('Произошла ошибка при загрузке файла. Проверьте файл и размер.');
            </script>";
        }
    }
}
